package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Brand;
import com.abaaba.project2.bean.BrandExample;
import com.abaaba.project2.bean.bo.BrandCreateBo;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.bean.vo.BrandCreateVo;
import com.abaaba.project2.mapper.BrandMapper;
import com.abaaba.project2.service.AdminBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class AdminBrandServiceImpl implements AdminBrandService {

    @Autowired
    BrandMapper brandMapper;

    @Override
    public BaseData<Brand> selectBrandBySort(Integer page, Integer limit, String sort, String order, Integer id, String name) {

        PageHelper.startPage(page, limit);

        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null && !("".equals(String.valueOf(id)))) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !("".equals(name))) {
            criteria.andNameLike("%" + name + "%");
        }

        brandExample.setOrderByClause(sort + " " + order);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo<Brand> brandPageInfo = new PageInfo<Brand>(brands);

        return BaseData.ok(brands, brandPageInfo.getTotal());
    }

    @Override
    public BrandCreateVo insertBrand(BrandCreateBo brandCreateBo) {
        Brand brand = new Brand();
        brand.setName(brandCreateBo.getName());
        brand.setDesc(brandCreateBo.getDesc());
        brand.setFloorPrice(brandCreateBo.getFloorPrice());
        brand.setPicUrl(brandCreateBo.getPicUrl());
        brand.setAddTime(new Date(System.currentTimeMillis()));
        brand.setUpdateTime(brand.getAddTime());
        brandMapper.insertSelective(brand);
        BrandCreateVo brandCreateVo = new BrandCreateVo(brand.getId(), brand.getName(), brand.getDesc(), brand.getPicUrl(), brand.getFloorPrice(), brand.getAddTime(), brand.getUpdateTime());
        return brandCreateVo;
    }

    @Override
    public int deleteBrand(Brand brand) {
        brand.setDeleted(true);
        return brandMapper.updateByPrimaryKey(brand);
    }

    @Override
    public int updateBrand(Brand brand) {
        return brandMapper.updateByPrimaryKey(brand);
    }
}
